package com.yc.mvc.web;

import com.yc.mvc.bean.Result;
import com.yc.mvc.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Controller // 默认返回视图(JSP,Thymeleaf)
public class UserAction {

    @GetMapping("/tologin")
    public String toLogin() {
        return "lgn";  //  默认请求转发 404
    }

    @GetMapping("/login")
    @ResponseBody  // ==> 返回JSON
    public String login(String name, String pwd) {
        return name + ":" + pwd;
    }

    @GetMapping("/tohome")
    public String tohome() {
        return "redirect:home.html"; // 响应重定向
    }

    @GetMapping("servlet.s")
    public void outServlet(
            HttpServletRequest req,
            HttpServletResponse resp,
            HttpSession session,
            PrintWriter out
    ) {
        // 设置响应内容类型  为下载文件
        resp.setHeader("Content-Type", "application/x-msdownload");
        // 设置下载文件的名称
        resp.setHeader("Content-Disposition", "attachment;filename=servlet.txt");
        out.println(req.toString());
        out.println(resp.toString());
        out.println(session.toString());
    }

    @ResponseBody
    @RequestMapping("reg.do")
    public Result reg(User user) {
        return new Result(0, "OK", user);
    }

    @ResponseBody
    @RequestMapping("add.s")
    public int add(@RequestParam(name="x", required = true) int a,
                   @RequestParam(value="y", required = false,
                        defaultValue = "10")int b) {
        return a + b;
    }

    // 地址参数
    @GetMapping("/add/{x}/{y}")
    @ResponseBody
    public Integer add1(@PathVariable("x") Integer a, @PathVariable("y")Integer b){
        return a+b;
    }

    @GetMapping("/showName/{name}")
    @ResponseBody
    public String showName(@PathVariable("name") String name){
        return name;
    }

    @GetMapping("/vals")
    @ResponseBody
    public Map<String,Object> getValue(
            @RequestHeader String Host,
            @RequestHeader("Accept") String accept,
            @RequestHeader("Cookie") String cookie,
            @CookieValue("JSESSIONID") String sessionId,
            @CookieValue(value="name",required = false) String name,
            @CookieValue(value="age",defaultValue="18") int age
    ){
        Map<String,Object> ret = new HashMap<>();
        ret.put("host",Host);
        ret.put("accept",accept);
        ret.put("cookie",cookie);
        ret.put("name",name);
        ret.put("age",age);
        return ret;
    }

    /**
     * 1. 被验证对象前加 @Value
     * 2. 被验证对象后加 Errors 收集验证结果
     */
    @PostMapping("register.do")
    @ResponseBody
    public Result register(@Valid User user, Errors errors){
        System.out.println(user.getBirthday());
        System.out.println(user.getRegtime());
        if(errors.hasErrors()){
            // 所有错误
            System.out.println(errors.getAllErrors());
            // 字段验证错误
            System.out.println(errors.getFieldErrors());
            // 全局错误
            System.out.println(errors.getGlobalErrors());
            return new Result(0, "验证错误", errors.getAllErrors());
        }
        return new Result(1, "成功", null);
    }

    @GetMapping("/")
    public String home(){
        return "redirect:home.html";
    }

    // 用户头像上传
    @PostMapping("upload.do")
    @ResponseBody
    public Result upload(@RequestParam("head") MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();// 上传的文件名
        System.out.println(file.getSize());// 文件大小
        System.out.println(file.getContentType());// 文件内容类型
        String diskpath = "D:\\_文件柜\\112\\0717\\upload\\" + filename;
        // 保存文件
        file.transferTo(new File(diskpath));
        String webpath = "upload/" + filename;
        return new Result(1,"文件上传成功", webpath);
    }



}
